--- title: "4、对称排序" created: 2025-11-28 tags: - 算法 --- # 4、对称排序 ## 题目 [对称排序](https://www.lanqiao.cn/problems/3790/learning/) ![[image-fb0dccb6.png]] ## 思路分析 简单题有简单题的写法 一开始聪明反被聪明误了 浪费蛮多时间的 1 2 3 4 4 3 2 1 一开始是从最终状态来考虑的 最终状态应该是排好序的 小的在下标小处 大的在下标大处 当时遗漏了一个点 就是数可能不是与下标数一一对应的 就写出了这种写法 ```cpp // #include // using namespace std; // #define endl '\n' // const int N=1e5+10; // int a[N]; // int main() // { // ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); // int n;cin>>n; // for(int i=1;i<=n;i++){ // cin>>a[i]; // } // for(int i=1;i<=n;i++){ // if(i!=a[i] || i!=a[(n+1)-i]){ // cout<<"NO"< using namespace std; #define endl '\n' const int N=1e5+10; int a[N]; int b[N]; int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int n;cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; memcpy(b,a,sizeof a); sort(b+1,b+1+n); for(int i=1;2*i<=n;i++){ if(a[i]>a[(n+1)-i]){ swap(a[i],a[(n+1)-i]); } } for(int i=1;i<=n;i++){ if(a[i]!=b[i]){ cout<<"NO"<